package com.thz.system.controller.video;

import com.thz.common.core.controller.BaseController;
import com.thz.common.core.result.Result;
import com.thz.system.pojo.blog.Blog;
import com.thz.system.pojo.blog.dto.UpdateBlogDTO;
import com.thz.system.pojo.video.Video;
import com.thz.system.service.video.IVideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description
 * @Author thz
 * @Date 2025/5/11
 */
@Slf4j
@RestController
@RequestMapping("/video")
@Tag(name = "视频接口")
public class VideoController extends BaseController {

    @Autowired
    private IVideoService videoService;

    @PostMapping("/upload")
    @Operation(summary = "上传视频")
    public Result<Video> uploadVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("courseId") Long courseId) {
        try {
            return IToResult(videoService.uploadVideo(file, title, content, courseId));
        } catch (Exception e) {
            log.error("Video upload failed", e);
            return Result.failed("视频上传失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<Video>> list(@NonNull @RequestParam("courseId") Long courseId){
        return Result.ok(videoService.list(courseId));
    }

    @PostMapping("/update")
    public Result update(@RequestParam("videoId") Long videoId,
                         @RequestParam("file") MultipartFile file,
                         @RequestParam("title") String title,
                         @RequestParam("content") String content){
        try {
            return UToResult(videoService.update(videoId, file, title, content));
        } catch (Exception e) {
            log.error("Video upload failed", e);
            return Result.failed("视频上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result delete(@NotNull @RequestParam("videoId") Long videoId){
        try {
            return UToResult(videoService.delete(videoId));
        } catch (Exception e) {
            log.error("Video upload failed", e);
            return Result.failed("视频上传失败：" + e.getMessage());
        }
    }

    @GetMapping("/{videoId}")
    @Operation(summary = "获取视频信息")
    public Result<Video> getVideo(@NotNull @RequestParam("videoId") Long videoId) {
        try {
            Video video = videoService.getVideoById(videoId);
            if (video == null) {
                return Result.failed("视频不存在");
            }
            return Result.ok(video);
        } catch (Exception e) {
            log.error("Failed to get video", e);
            return Result.failed("获取视频信息失败：" + e.getMessage());
        }
    }
}
